package Day15;

import java.util.Iterator;
import java.util.TreeSet;

public class DoBook {
    private TreeSet<Book> set = new TreeSet<>();
    User u = new User("000000","library");
    public boolean login(String s,String m){
        if(s.equals(u.getAccount())){
            if(m.equals(u.getPassword())){
                return true;
            }
        }
        return false;
    }

    //图书新增
    public boolean addBook(Book e){
        boolean a = set.add(e);
        return a;
    }

    //图书删除
    public void deleteBook(String name){
        Book b = findBook(name);
        if(b == null){
            System.out.println("没找到该图书");
        }else{
            boolean c = set.remove(b);
            if(c == true){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }

    }

    //图书修改
    public void reviseBook(String name){
        View v = new View();
        Book b1 = findBook(name);
        Book b = v.reviseBook();
        if(b1 == null){
            System.out.println("找不到该信息");
        }else{
            addBook(b);
            boolean bo = set.remove(b1);
            if(bo == true){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        }
    }

    //图书查找
    public Book findBook(String name){

        for (Book b:set) {
            if(name.equals(b.getName())){
                return b;
            }
        }
        return null;
    }
    //打印图书
    public void printfBook(Book b){
        System.out.println(b.toString());
    }
//升序
    public void printAllRise(){
        if(set.size() == 0) {
            System.out.println("暂无信息");
        }else {
            Iterator<Book> iterator = set.iterator();
//            for (Book b : set) {
//                System.out.println(b);
//            }
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
        }
    }
    //降序
    public void printAllLower(){
        if(set.size() == 0){
            System.out.println("暂无信息");
        }else {
            Iterator<Book> iterator = set.descendingIterator();
//            for (Book b : set) {
//                System.out.println(b);
//            }
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
        }
    }
}
